package com.bskj.bms.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.bms.bean.req.BillListReqVo;
import com.bskj.bms.bean.vo.PropCustRespVo;
import com.bskj.bms.service.IBmsIncomeService;
import com.bskj.bms.service.IBmsPropCustService;
import com.bskj.common.bean.model.AggregateAmountsVo;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.bean.model.PropCust;
import com.bskj.common.dao.ParkingLotCustomerDao;
import com.bskj.common.dao.PropCustDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.Page;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.service.impl.BaseServiceImpl;

@Service
public class BmsPropCustServiceImpl extends BaseServiceImpl<PropCust> implements IBmsPropCustService {

	@Autowired
	private PropCustDao propCustDao;

	@Autowired
	private IBmsIncomeService bmsIncomeService;

	@Autowired
	private ParkingLotCustomerDao parkingLotCustomerDao;

	@Override
	public BaseDao<PropCust> getBaseDao() {
		// TODO Auto-generated method stub
		return propCustDao;
	}

	@Override
	public Page<PropCustRespVo> getPropCustList(BillListReqVo req) throws Exception {
		Page<PropCustRespVo> propCustRespVoPage = new Page<>();
		if(req.getCustId() != 0){
			List<PropCustRespVo> propCustRespVoList = new ArrayList<>();
			PropCustRespVo propCustRespVo = new PropCustRespVo();
			ParkingLotCustomer parkingLotCustomer = parkingLotCustomerDao.get(req.getCustId());
			propCustRespVo.setParkName(parkingLotCustomer.getParkingName());
			List<AggregateAmountsVo> aggregateAmountsVos = bmsIncomeService.FindAggregateAmount(req);
			if (aggregateAmountsVos != null && aggregateAmountsVos.size() >= 3) {
				propCustRespVo.setTotalMoney(
						aggregateAmountsVos.get(0).getSum() + aggregateAmountsVos.get(1).getSum());
				propCustRespVo.setCashMoney(aggregateAmountsVos.get(0).getSum());
				propCustRespVo.setWxMoney(aggregateAmountsVos.get(1).getSum());
				propCustRespVo.setCouponMoney(aggregateAmountsVos.get(2).getSum());
				propCustRespVo.setCustId(req.getCustId());
			}
			
			propCustRespVoList.add(propCustRespVo);
			propCustRespVoPage.setList(propCustRespVoList);
			return propCustRespVoPage;
		}
		FindRequestEntity findReq = new FindRequestEntity();
		findReq.addWhere("prop_id", WhereEnum.EQ, req.getPropId());
		findReq.setPage(req.getPageNo(), req.getPageSize());
		Page<PropCust> propCustPage = page(findReq);
		List<PropCust> propCustlist = ((Page<PropCust>) propCustPage).getList();
		List<PropCustRespVo> propCustRespVoList = new ArrayList<>();
		for (PropCust propCust : propCustlist) {
			PropCustRespVo propCustRespVo = new PropCustRespVo();
			findReq = new FindRequestEntity();
			req.setCustId(propCust.getCustId());
			ParkingLotCustomer parkingLotCustomer = parkingLotCustomerDao.get(propCust.getCustId());
			propCustRespVo.setParkName(parkingLotCustomer.getParkingName());
			propCustRespVo.setCustId(propCust.getCustId());
			List<AggregateAmountsVo> aggregateAmountsVos = bmsIncomeService.FindAggregateAmount(req);
			if (aggregateAmountsVos != null && aggregateAmountsVos.size() >= 3) {
				propCustRespVo.setTotalMoney(
						aggregateAmountsVos.get(0).getSum() + aggregateAmountsVos.get(1).getSum());
				propCustRespVo.setCashMoney(aggregateAmountsVos.get(0).getSum());
				propCustRespVo.setWxMoney(aggregateAmountsVos.get(1).getSum());
				propCustRespVo.setCouponMoney(aggregateAmountsVos.get(2).getSum());
			}
			propCustRespVoList.add(propCustRespVo);
		}
		propCustRespVoPage.setList(propCustRespVoList);
		propCustRespVoPage.setPage(propCustPage.getPage());
		propCustRespVoPage.setPageSize(propCustPage.getPageSize());
		propCustRespVoPage.setTotalRow(propCustPage.getTotalRow());
		return propCustRespVoPage;
	}
}
